package io.renren.sf.controller;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.annotation.SysLog;
import io.renren.common.utils.PageUtil;
import io.renren.common.utils.RandomUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import io.renren.sf.entity.ActivecodeEntity;
import io.renren.sf.service.ActivecodeService;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author cyl_txw
 * @email cyl_txw@gmail.com
 * @date 2021-04-22 13:29:02
 */
@RestController
@RequestMapping("sf/activecode")
public class ActivecodeController {
    @Autowired
    private ActivecodeService activecodeService;

    /**
     * 批量新增cdk
     */
    @SysLog("批量新增激活码")
    @GetMapping("/saveBatchCdk")
    public R saveBatchCdk(@RequestParam("num") Integer num, @RequestParam("stoptime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date stoptime){
        if((stoptime.compareTo(new Date())) < 0){
            return R.error("到期时间小于当前时间");
        }
        List<ActivecodeEntity> cdkList = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            ActivecodeEntity activecodeEntity = new ActivecodeEntity();
            // 8位激活码
            String cdk = RandomUtils.randomUtil();
            activecodeEntity.setCdk(cdk);
            activecodeEntity.setStarttime(new Date());
            activecodeEntity.setStatus(0);
            activecodeEntity.setStoptime(stoptime);
            cdkList.add(activecodeEntity);
        }
        activecodeService.saveBatch(cdkList);
        return R.ok();
    }
    /**
     * 获取cdk列表
     */
    @GetMapping("/getCdkList")
    public R getCdkList(@RequestParam Map<String, Object> params){
        // 如果到期就改变激活码状态
        activecodeService.updateStatus();
        String status = (String) params.get("status");
        QueryWrapper<ActivecodeEntity> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(status)){
            queryWrapper.eq("status", status);
        }
        long page = Long.parseLong((String) params.get("page"));
        long limit = Long.parseLong((String) params.get("limit"));
        Page<ActivecodeEntity> cdkPage = new Page<>(page, limit);
        activecodeService.page(cdkPage, queryWrapper);
        return R.ok().put("cdkPage", cdkPage);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("sf:activecode:info")
    public R info(@PathVariable("id") Long id){
		ActivecodeEntity activecode = activecodeService.getById(id);

        return R.ok().put("activecode", activecode);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("sf:activecode:save")
    public R save(@RequestBody ActivecodeEntity activecode){
		activecodeService.save(activecode);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("sf:activecode:update")
    public R update(@RequestBody ActivecodeEntity activecode){
        // 到期时间小于当前时间就禁用
        if((activecode.getStoptime().compareTo(new Date())) < 0){
            activecode.setStatus(2);
        }
		activecodeService.updateById(activecode);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("sf:activecode:delete")
    public R delete(@RequestBody Long[] ids){
		activecodeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
